﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_bug_AddingBug : System.Web.UI.Page
{
    User currentUser;

    /// <summary>
    /// pass a user to this instance
    /// </summary>
    /// <param name="currentUsername">current logged in username</param>
    public admin_bug_AddingBug(string currentUsername)
    {
        this.currentUser = UserManagement.GetUser(currentUsername);
    }

    /// <summary>
    /// the current logged in user will be used for this instance
    /// </summary>
    public admin_bug_AddingBug()
    {
        this.currentUser = UserManagement.GetCurrentUser();
    }

    /// <summary>
    /// Current user that this instance is working on
    /// </summary>
    public User CurrentUser
    {
        get
        {
            return this.currentUser;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // adding items for type of bugs
            foreach (var item in EnumUtility.EnumToList<BugManagement.Type>())
            {
                DDLTypeOfBug.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            // Adding items for Severity
            foreach (var item in EnumUtility.EnumToList<BugManagement.Severity>())
            {
                dllSeverity.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            // Adding items for Priority
            foreach (var item in EnumUtility.EnumToList<BugManagement.Priority>())
            {
                dllPriority.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            // Adding items for reproducabillity
            foreach (var item in EnumUtility.EnumToList<BugManagement.Reproducability>())
            {
                dllReproducability.Items.Add(EnumUtility.GetEnumNiceName(item));
            }

            var projectname = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects.Where(i => i.Username == currentUser.Username)
                              select item.Project.Name;

            foreach (var item in projectname)
            {
                dllProjectName.Items.Add(item);
            }
        }
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        // Get the id of a project.
        int id = (from item in ApplicationGlobalVariables.KillDaBugsDatabase.Projects.Where(i => i.Name == dllProjectName.Text)
                  select Convert.ToInt16(item.ProjectId)).Single();

        BugManagement objBugManagement = new BugManagement(currentUser);
        //try
        //{
        int result = objBugManagement.AddBug(id, DDLTypeOfBug.SelectedIndex, textSummary.Text, dllReproducability.SelectedIndex, txtReproductionSteps.Text, txtInVersion.Text, txtFoundInComponent.Text, dllSeverity.SelectedIndex, dllPriority.SelectedIndex, txtDescription.Text);
        if (result != -1)
        {
            mess.Text = "Bug Submitted successfully!";
        }
        else
        {
            mess.Text = "Cannot submit bug, maybe due to database error!";
        }
        //}
        //catch (Exception ex)
        //{
        //    mess.Text = "<div>" + ex.Message + "</div>";
        //}

        //mess.Text = "<div><strong>" + id + "</strong><br/>" + DDLTypeOfBug.SelectedIndex + "<br/>" +
        //    textSummary.Text + "<br/>" + dllReproducability.SelectedIndex + "<br/>" + txtReproductionSteps.Text + "<br/>" +
        //   dllSeverity.SelectedIndex + "<br/>" + dllPriority.SelectedIndex + "<br/>" + txtFoundInComponent.Text + "<br/>" + txtInVersion.Text + "<br/>" + "</div>";

        //end
    }
}